<?php

function meinianParse($filename) {

    $content = file_get_contents($filename);
    //$content = mb_convert_encoding($content, 'UTF-8', 'GB2312,GBK,UTF-8');
    $encode = mb_detect_encoding($content, array("ASCII", 'UTF-8', 'GB2312', "GBK"));
//    if ($encode == "EUC-CN") {
//        $content = iconv("GB2312", "UTF-8", $content);
//    }
    $data = array();  //所有内容

    /*
     * 获取基本信息
     */
    $pattern_basic = "/个人基本信息(.*)<td height=\"1\" bgcolor=\"#d1d1d1\">/isU";
    // var_dump($pattern_basic);exit();
    preg_match($pattern_basic, $content, $matches);
    $result_basic = $matches[0];
    //获取基本信息中小项目名称
    //$pattern_basic_item_name="/<td align=\"left\" class=\"brown02_12\">(.*)<\/td>/isU";
    $pattern_basic_item_name = "/(<td align=\"left\" class=\"brown02_12\">|<td height=\"30\" align=\"left\" class=\"brown02_12\">)(.*)<\/td>/isU";

    preg_match_all($pattern_basic_item_name, $result_basic, $matches);
    $array_basic_item_name = array();
    foreach ($matches[0] as $key => $val) {
        array_push($array_basic_item_name, trim(strip_tags($val)));
    }
    //获取基本信息中小项目的值
    $pattern_basic_item_value = "/<td align=\"left\" class=\"black_12\">(.*)<\/td>/isU";
    preg_match_all($pattern_basic_item_value, $result_basic, $matches);
    $array_basic_item_value = array();
    foreach ($matches[0] as $key => $val) {
        array_push($array_basic_item_value, trim(strip_tags($val)));
    }

    //把key和value进行组合
    $basic_itemSet = array();
    $ret_basic = array_combine($array_basic_item_name, $array_basic_item_value);
    foreach ($ret_basic as $key => $val) {
        array_push($basic_itemSet, array("itemName" => $key, "itemValue" => $val));
    }
//	var_dump($basic_itemSet);exit();
    array_push($data, array("parentName" => "基本信息", "itemSet" => $basic_itemSet));
//	var_dump($basic_itemSet);exit();
    //整理出用户基本信息，为了入库
    $reportinfo = array('realname' => '', 'gender' => '', 'examno' => '', 'examdate' => '');
    foreach ($basic_itemSet as $key => $val) {
//			var_dump(substr($val['itemName'],0,9));
        if (substr($val["itemName"], 0, 6) == "姓名") {
            $reportinfo['realname'] = $val["itemValue"];
        }

        if (substr($val["itemName"], 0, 6) == "性别") {
            $reportinfo['gender'] = $val["itemValue"];
//					var_dump($reportinfo['gender']);exit();
        }

        if (substr($val['itemName'], 0, 9) == "体检号") {
            $reportinfo["examno"] = $val['itemValue'];
        }

        if (substr($val['itemName'], 0, 12) == "体检时间") {
            $reportinfo["examdate"] = $val['itemValue'];
        }
    }

    $patten = "/<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">(.*)<td align=\"left\">/isU";

    preg_match_all($patten, $content, $matches);
    $result_large_project = $matches[0];

    /*
     * 处理各个体检大项目的内容
     */
    foreach ($result_large_project as $key => $val) {

        //筛选出每一体检项目的parentName
        //匹配小项目套餐
        $pattern_parentName = "/(<td class=\"brown02_14\" style=\"color: #ED9C72\">(.*)<\/a><\/td>)|(<span id=\"Repeater1_ct(.*)<\/span><\/td>)/isU";
        preg_match_all($pattern_parentName, $val, $matches);
//		var_dump($pattern_parentName);continue;
        if (isset($matches[0][0])) {
            $parentName = strip_tags($matches[0][0]);

            //筛选出每一小项html
            $pattern_tr = "/<tr>(.*)<\/tr>/isU";
            preg_match_all($pattern_tr, $val, $matches);
            //筛选出每一个体检小项目中的指标的值
            $itemSet = array();
            foreach ($matches[0] as $key => $val) {
                //  var_dump($val); continue;
                $patten_td = "/<td align(.*)<\/td>/isU";
                preg_match_all($patten_td, $val, $matches);
                //去掉每个小项值中的html标签
                $result_td = array();
                foreach ($matches[0] as $key => $val) {
                    array_push($result_td, strip_tags($val));
                }

                if (array_key_exists(1, $result_td)) {
                    if (substr($result_td["1"], -3) == "↑" || substr($result_td["1"], -3) == "↓") {
                        $ex = explode(" ", $result_td["1"]);
                        array_slice($result_td, 1, 1);
                        $result_td[1] = $ex[0];
                        $result_td[4] = $ex[1];
                    }
                }

                if (count($result_td) == 4) {
                    $result_combine = array_combine(array("itemName", "itemValue", "unit", "normal"), $result_td);
                    array_push($itemSet, $result_combine);
                }

                if (count($result_td) == 5) {
                    $result_combine = array_combine(array("itemName", "itemValue", "unit", "normal", "issue"), $result_td);
                    array_push($itemSet, $result_combine);
                }
            }

            $result = array("parentName" => $parentName, "itemSet" => $itemSet);
            array_push($data, $result);
        }
        unset($pattern_parentName);
    }
    foreach ($data[0]['itemSet'] as $key => $value) {
        if (empty(str_replace("&nbsp;", "", $value['itemName'])) && empty(str_replace("&nbsp;", "", $value['itemValue']))) {
            unset($data[0]['itemSet'][$key]);
        }
    }

    //筛选出“总检”内容
    //$patter_summary="/\* 您好，欢迎您来到美年大(.*)<\/td>/isU";
    $patter_summary = "/\*(.*)<\/td>/isU";
    preg_match($patter_summary, $content, $matches);
    array_push($data, array("parentName" => '总检',
        "itemSet" => array(
            array("itemName" => "总检", "itemValue" => $matches[1])
        ),
    ));
    $json = json_encode_ex($data);
    $json = str_replace('\r\n\r\n', '', $json);

    $json = str_replace('\&nbsp;', '', $json);

    $qian = array(" ", "　", "\t", "\n", "\r", "&nbsp;", "：");
    $hou = array("", "", "", "", "", "", "");
    $json = str_replace($qian, $hou, $json);
    $data = json_decode($json, true);
    return json_encode_ex(array("data" => $data, "reportinfo" => $reportinfo));
}
